package com.example.springboot_shiro.service.impl;

import com.example.springboot_shiro.domain.test.*;
import com.example.springboot_shiro.domain.test.Authority;
import com.example.springboot_shiro.domain.test.Role;
import com.example.springboot_shiro.domain.test.RoleAuthority;
import com.example.springboot_shiro.domain.test.RoleUser;
import com.example.springboot_shiro.mapper.test.*;
import com.example.springboot_shiro.service.UserService;
import com.example.springboot_shiro.utils.SaltUtils;
import lombok.AllArgsConstructor;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: lqy
 * @date: 2021/9/8 14:11
 * @description:
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserService {

    private final UserMapper userMapper;

    private final RoleUserMapper roleUserMapper;

    private final RoleMapper roleMapper;

    private final RoleAuthorityMapper roleAuthorityMapper;

    private final AuthorityMapper authorityMapper;

    @Override
    public User findByUserName(String userName) {
        UserExample userExample = new UserExample();
        userExample.createCriteria().andUsernameEqualTo(userName);
        List<User> userList = userMapper.selectByExample(userExample);
        if (userList.size() != 0) {
            return userList.get(0);
        }
        return new User();
    }

    @Override
    public int register(User user) {

        // 处理业务调用mapper

        //1.生成随机盐
        String salt = SaltUtils.getSalt(8);
        //2.将随机盐保存到数据库
        user.setSalt(salt);
        //3.明文密码进行md5 +salt +hash 散列
        Md5Hash md5Hash = new Md5Hash(user.getPassword(), salt, 1024);
        user.setPassword(md5Hash.toHex());
        return userMapper.insert(user);
    }

    /**
     * 查询用户角色列表
     *
     * @param userName
     * @return
     */
    @Override
    public List<Role> findRolesByUserId(String userName) {
        UserExample userExample = new UserExample();
        RoleUserExample roleUserExample = new RoleUserExample();
        userExample.createCriteria().andUsernameEqualTo(userName);
        List<RoleUser> roleUsers = new ArrayList<>();
        List<User> userList = userMapper.selectByExample(userExample);
        User user = new User();
        if (userList.size() != 0) {
            user = userList.get(0);
            roleUserExample.createCriteria().andUserIdEqualTo(user.getId());
            roleUsers = roleUserMapper.selectByExample(roleUserExample);
        }
        List<Role> roleList = new ArrayList<>();
        List<Authority> authorityList = new ArrayList<>();
        for (RoleUser roleUser : roleUsers) {
            Role role = roleMapper.selectByPrimaryKey(roleUser.getRoleId());
            RoleAuthorityExample roleAuthorityExample = new RoleAuthorityExample();
            roleAuthorityExample.createCriteria().andRoleIdEqualTo(role.getRoleId());
            List<RoleAuthority> roleAuthorities = roleAuthorityMapper.selectByExample(roleAuthorityExample);
            for (RoleAuthority authority : roleAuthorities) {
                Authority temp = authorityMapper.selectByPrimaryKey(authority.getAuthorityId());
                authorityList.add(temp);
            }
            role.setAuthorities(authorityList);
            roleList.add(role);
        }
        return roleList;
    }
}
